﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InSafetyAirClient.GUI.Flight
{
    public partial class UpdateF : UserControl
    {
        InSafetyAirService.Flight f = null;
        List<InSafetyAirService.FlightSegment> segments = new List<InSafetyAirService.FlightSegment>();
        List<InSafetyAirService.Schedule> shedules = new List<InSafetyAirService.Schedule>();
        List<InSafetyAirService.Flight> flights = new List<InSafetyAirService.Flight>();
        DateTime sDate = new DateTime();
        DateTime eDate = new DateTime();
        List<InSafetyAirClient.InSafetyAirService.Airport> airports = null;
        List<InSafetyAirClient.InSafetyAirService.Plane> planes = null;
        List<InSafetyAirClient.InSafetyAirService.AirLine> airlines = null;
        DataTable dataTable1 = new DataTable();
        DataTable dataTable2 = new DataTable();
        AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();
        private bool hus = true;

        public UpdateF()
        {
            InitializeComponent();
        }

        private void UpdateF_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                populateList();
            }
        }

        private void populateList()
        {
            dataTable1.Columns.Add("Id", typeof(int));
            dataTable1.Columns.Add("Start date", typeof(DateTime));
            dataTable1.Columns.Add("End date", typeof(DateTime));
            dataTable1.Columns.Add("Price", typeof(string));
            dataTable1.Columns.Add("Repeats", typeof(int));
            dataTable1.Columns.Add("For", typeof(int));
            //
            dataTable2.Columns.Add("Id", typeof(int));
            dataTable2.Columns.Add("Start Airport", typeof(string));
            dataTable2.Columns.Add("End Airport", typeof(string));
            comboBox1.Items.Clear();
            ServiceSingle servS = ServiceSingle.Instance;
            flights = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.Flight).Name).OfType<InSafetyAirClient.InSafetyAirService.Flight>().ToList();
            foreach (InSafetyAirService.Flight airline in flights)
            {
                comboBox5.Items.Add(airline.flightNumber);
                autoComplete.Add(airline.flightNumber);
            }
            comboBox5.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox5.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox5.AutoCompleteCustomSource = autoComplete;
            airlines = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.AirLine).Name).OfType<InSafetyAirClient.InSafetyAirService.AirLine>().ToList();
            foreach (InSafetyAirService.AirLine airline in airlines)
            {
                comboBox1.Items.Add(airline.name);
                autoComplete.Add(airline.name);
            }
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox1.AutoCompleteCustomSource = autoComplete;
            planes = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.Plane).Name).OfType<InSafetyAirClient.InSafetyAirService.Plane>().ToList();
            foreach (InSafetyAirService.Plane plane in planes)
            {
                comboBox2.Items.Add(plane.registrationNo);
                autoComplete.Add(plane.registrationNo);
            }
            comboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox2.AutoCompleteCustomSource = autoComplete;
            airports = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.Airport).Name).OfType<InSafetyAirClient.InSafetyAirService.Airport>().ToList();
            foreach (InSafetyAirService.Airport airport in airports)
            {
                comboBox3.Items.Add(airport.name);
                comboBox4.Items.Add(airport.name);
                autoComplete.Add(airport.name);
            }
            comboBox3.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox3.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox3.AutoCompleteCustomSource = autoComplete;
            comboBox4.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox4.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox4.AutoCompleteCustomSource = autoComplete;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            foreach (InSafetyAirService.Flight flight in flights)
            {
                dataTable2.Clear();
                if (flight.flightNumber.Equals(comboBox5.Text))
                {
                    f = flight;
                    foreach (InSafetyAirService.AirLine airline in airlines)
                    {
                        if (airline.airLineId == flight.airLineId)
                        {
                            comboBox1.Text = airline.name;
                        }
                    }
                    foreach (InSafetyAirService.Plane plane in planes)
                    {
                        if (plane.planeId == flight.planeId)
                        {
                            comboBox2.Text = plane.registrationNo;
                        }
                    }
                    textBox1.Text = flight.flightNumber;
                    segments= ServiceSingle.Instance.getService().getFlightSegments(flight.flightId).ToList();
                    foreach (InSafetyAirService.FlightSegment fs in segments)
                    {
                        InSafetyAirService.Airport sair = new InSafetyAirService.Airport();
                        InSafetyAirService.Airport eair = new InSafetyAirService.Airport();
                        foreach (InSafetyAirService.Airport airp in airports)
                        {
                            if (airp.airportId == fs.startAirportId)
                                 sair = airp;
                            if (airp.airportId == fs.endAirportId)
                                eair = airp;
                        }
                        dataTable2.Rows.Add(fs.flightSegmentId, sair.name, eair.name);
                    }
                    dataGridView1.DataSource = dataTable2;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            hus = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            hus = false;
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            if (hus)
            {
                textBox2.Text = monthCalendar1.SelectionRange.Start.ToString();
                sDate = monthCalendar1.SelectionRange.Start;
            }
            else
            {
                textBox3.Text = monthCalendar1.SelectionRange.Start.ToString();
                eDate = monthCalendar1.SelectionRange.Start;
            }
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dataTable1.Clear();
            if (dataGridView1.SelectedRows[0] != null)
            {
                label13.Text = "Working on " + dataGridView1.SelectedRows[0].Cells[1].Value + " to " + dataGridView1.SelectedRows[0].Cells[2].Value;
            }
            foreach (InSafetyAirService.FlightSegment fs in segments)
            {
                if (fs.flightSegmentId ==(int) dataGridView1.SelectedRows[0].Cells[0].Value)
                {
                    comboBox3.Text = dataGridView1.SelectedRows[0].Cells[1].Value+"";
                    comboBox4.Text = dataGridView1.SelectedRows[0].Cells[2].Value+"";
                    shedules = ServiceSingle.Instance.getService().getSchedules(fs.flightSegmentId).ToList();
                    foreach (InSafetyAirService.Schedule shed in shedules)
                    {
                        dataTable1.Rows.Add(shed.scheduleId, shed.startTime, shed.endTime, shed.price, 0, 0);
                    }
                    dataGridView2.DataSource = dataTable1;
                }
            }
        }

        private void dataGridView2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            textBox4.Text = dataGridView2.SelectedRows[0].Cells[3].Value + "";
            textBox2.Text = dataGridView2.SelectedRows[0].Cells[1].Value + "";
            textBox3.Text = dataGridView2.SelectedRows[0].Cells[2].Value + "";
            sDate = (DateTime)dataGridView2.Rows[0].Cells[1].Value;
            eDate = (DateTime)dataGridView2.Rows[0].Cells[2].Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sDate = sDate.AddHours(System.Convert.ToInt32(numericUpDown1.Value));
            sDate = sDate.AddMinutes(System.Convert.ToInt32(numericUpDown2.Value));
            eDate = sDate.AddHours(System.Convert.ToInt32(numericUpDown4.Value));
            eDate = sDate.AddMinutes(System.Convert.ToInt32(numericUpDown3.Value));
            if (sDate < eDate)
            {
                int id= (int)dataGridView2.Rows[0].Cells[0].Value;
                InSafetyAirService.Schedule sh = (InSafetyAirService.Schedule)ServiceSingle.Instance.getService().GetById("Schedule",id);
                sh.startTime = sDate;
                sh.endTime = eDate;
                sh.price = System.Convert.ToInt32(textBox4.Text);
                ServiceSingle.Instance.getService().updateSchedule(sh);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int id = (int)dataGridView1.Rows[0].Cells[0].Value;
            InSafetyAirService.FlightSegment fs = (InSafetyAirService.FlightSegment)ServiceSingle.Instance.getService().GetById("FlightSegment", id);
            foreach (InSafetyAirService.Airport air in airports)
            {
                if (air.name.Equals(comboBox3.Text))
                    fs.startAirportId = air.airportId;
                if (air.name.Equals(comboBox4.Text))
                    fs.endAirportId = air.airportId;
            }
            fs.flightId = f.flightId;
            ServiceSingle.Instance.getService().updateSegment(fs);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            foreach (InSafetyAirService.AirLine air in airlines)
            {
                if (air.name.Equals(comboBox1.Text))
                    f.airLineId = air.airLineId;
            }
            foreach (InSafetyAirService.Plane plane in planes)
            {
                if (plane.registrationNo.Equals(comboBox2.Text))
                    f.planeId = plane.planeId;
            }
            f.flightNumber = textBox1.Text;
            ServiceSingle.Instance.getService().updateFlight(f);
        }
    }
}
